package com.kizuki.algorithm.tree;

import org.junit.Test;

import java.util.Stack;

public class TestTree {

    @Test
    public void orderTest() {
        String[] input = new String[]{"A", "B", "C", "D", "#", "F", "G", "H"};
        order(input);
    }

    public static void order(String[] a) {
        Stack<Integer> stack = new Stack<>();
        Integer root = 0;
        int len = a.length;
        stack.push(root);
        while(!stack.empty()) {
            root = stack.pop();
            System.out.print(a[root] + " ");

            int lc = 2 * root + 1;
            int rc = 2 * root + 2;
            if (rc < len && !a[rc].equals("#")) {
                stack.push(rc);
            }
            if (lc < len && !a[lc].equals("#")) {
                stack.push(lc);
            }
        }
    }

}
